package cn.acyou.leo.example.config;

import cn.acyou.leo.framework.interceptor.SpringMvcInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author youfang
 */
@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private SpringMvcInterceptor springMvcInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> filterPath = new ArrayList<>();
        /* Swagger ui  */
        filterPath.add("/swagger-ui.html");
        filterPath.add("/swagger-resources/**");
        filterPath.add("/swagger-ui.html");
        filterPath.add("/configuration/ui");
        filterPath.add("/swagger-resources");
        filterPath.add("/configuration/security");
        filterPath.add("/v2/api-docs");
        filterPath.add("/error");
        filterPath.add("/webjars/**");
        filterPath.add("/favicon.ico");
        registry.addInterceptor(springMvcInterceptor).excludePathPatterns(filterPath);
    }

    @Bean
    public SpringMvcInterceptor springMvcInterceptor() {
        return new SpringMvcInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //静态资源文件映射，然后就可以直接访问
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");

    }

}
